export default {
	namespaced: true,
	
	//数据
	state: () => ({
		token: uni.getStorageSync('token') || '',
		//用户信息对象
		userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
		//用户名字，身份证等信息
		personInfo: JSON.parse(uni.getStorageSync('personInfo') || '{}')
	}),
	
	//方法
	mutations: {
		
		//更新用户微信信息对象
		updateUserInfo(state, userinfo) {
			state.userinfo = userinfo
			
			this.commit('m_user/saveUserInfoToStorage')
		},
		//将用户微信信息对象持久化到本地
		saveUserInfoToStorage(state) {
			uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
		},
		//更新用户token
		updateToken(state, token) {
			state.token = token
			
			//持久化
			this.commit('m_user/saveTokenToStorage')
		},
		//持久化token到本地
		saveTokenToStorage(state) {
			uni.setStorageSync('token', state.token)
		},
		
		//更新用户信息对象
		updatePersonInfo(state, personInfo) {
			state.personInfo = personInfo
			
			this.commit('m_user/savePersonInfoToStorage')
			this.commit('m_user/savePersonInfoDataBase')
		},
		//将用户信息对象持久化到本地
		savePersonInfoToStorage(state) {
			uni.setStorageSync('personInfo', JSON.stringify(state.personInfo))
		},
		async savePersonInfoDataBase(state) {
			const {data: res} = await uni.$http.post('/visitor/save', state.personInfo)
			console.log("返回结果：",res)
			const tokenStr = res.token 
			this.commit('m_user/updateToken', tokenStr)
			if(res.msg === 'success') return uni.$showMsg('添加个人信息成功')
		}
		
	},

	getters: {
		
	}
		
}